<template>
	<view class="container">
		<uni-nav-bar fixed="true" statusBar="true" @clickLeft="backClick" color="#303133" leftIcon="back" background="#fff">
			<view class="searchBox">
				<icon class="searchIcon" type="search" :size='20' color='#999'></icon>
				<input class="topSearch" type="text" confirm-type="search" v-model="keys" placeholder="请输入服务卡名称" @confirm="search" />
			</view>
		</uni-nav-bar>
		<view class="content">
			<!-- #ifdef MP-WEIXIN -->
			<view class="tabBox" :style="{top:fixTop + 6 + 'px'}">
				<!-- #endif -->
				<!-- #ifndef MP-WEIXIN -->
				<view class="tabBox">
					<!-- #endif -->
					<view v-for="(item,index) in tabList" :key="index" :class="[1==1?'tab':'',current == index?'active':'']" @tap="changeTab(index)">{{item}}</view>
				</view>
				<!-- 空白页 -->
				<empty v-if="emptyShow" :custom="true" src="../../static/images/no-orders.png" text="您还没有相关记录哦"></empty>

				<!-- 订单列表 -->
				<view v-if="current == 0" v-for="(items,index) in cardList" :key="index" hover-class="none">
					<view class="order-item">
						<view class="i-top b-b">
							<text class="seller_name clamp">{{items.list.name}}</text>
							<text class="state" v-if="items.list.service_status == 0">待支付</text>
							<text class="state" v-else-if="items.list.service_status == 1">未使用</text>
							<text class="state" v-else-if="items.list.service_status == 2">使用中</text>
							<text class="state" v-else-if="items.list.service_status == 3">已结束</text>
						</view>
						<view class="cardType">
							<text class="a">{{items.list.cate_name}} :</text>
							<text class="b">{{items.list.cate_desc}}</text>
						</view>
						<view class="price">
							<text class="a" v-if="items.list.service_status == 0">待支付:</text>
							<text class="a" v-else>支付金额:</text>
							<text class="b">{{items.list.pay_price}}</text>
						</view>
						<view class="action-box b-t">
							<view class="action-btn" @tap.stop="cancelOrder(items.order_id)" v-if="items.list.service_status == 0">取消订单</view>
							<view class="action-btn" @tap.stop="phoneCall(items.online_phone)">联系客服</view>
							<view class="action-btn" @tap="goDetail(items)">订单详情</view>
							<view class="action-btn act" @tap.stop="goPay(items.order_id)" v-if="items.list.service_status == 0">立即支付</view>
						</view>
					</view>
				</view>

				<view v-if="current == 1" v-for="(items,index) in cardList" :key="index" hover-class="none">
					<view class="serviceRecod">
						<image class="cardbg" :src="items.image" mode=""></image>
						<view class="i-top b-b">
							<text class="seller_name clamp">{{items.name}}</text>
							<text class="b" v-if="items.cate == 1">{{items.sur_num}}</text>
							<text class="b" v-if="items.cate == 2">到期时间：{{items.cate_desc}}</text>
						</view>
						<view class="timeGroup">
							<view class="a">
								<text class="a-a">服务使用时间：</text>
								<text class="a-b">{{items.apply_time}}</text>
							</view>
							<view class="a">
								<text class="a-a">服务完成时间：</text>
								<text class="a-b">{{items.end_time}}</text>
							</view>
						</view>
					</view>
				</view>

				<uni-load-more class="loadMore" iconType="circle" :status="loadStatus" :contentText="contentText"></uni-load-more>
			</view>
			<!-- app选择支付方式 -->
			<choosePay ref="choosePay" @confirm="confirm"></choosePay>
		</view>
</template>

<script>
	import {
		serviceUseRecord,
		servicePay,
		serviceCancelOrder
	} from '@/api/user.js'
	import uniNavBar from "@/components/uni-nav-bar/uni-nav-bar.vue"
	import uniLoadMore from '@/components/uni-load-more/uni-load-more.vue';
	import empty from "@/components/empty/empty";
	import wechat from '@/common/wechat.js'
	import choosePay from '@/components/choosePay/choosePay.vue'
	export default {
		components: {
			uniNavBar,
			uniLoadMore,
			empty,
			choosePay
		},
		data() {
			return {
				type: '',
				//#ifdef MP-WEIXIN
				fixTop: getApp().globalData.MenuButtonBottom,
				//#endif
				tabList: ['服务卡记录', '服务记录'],
				current: 0,
				emptyShow: false,
				limit: 10,
				page: 1,
				keys: '',
				cardList: [],
				loadStatus: "noMore",
				contentText: {
					contentdown: "上拉显示更多",
					contentrefresh: "正在加载...",
					contentnomore: ""
				},
				paytype: 1,
				provider: "wxpay",
				// #ifdef MP-WEIXIN
				payStatus: "routine",
				// #endif
				// #ifdef APP-PLUS
				payStatus: "app",
				// #endif
				// #ifdef H5
				payStatus: "weixin",
				// #endif
				order_id: ''
			};
		},
		onLoad() {
			this.getCardList()
		},
		onShow() {
			if (uni.getStorageSync("paySuccess")) {
				this.loadStatus = "loading";
				this.page = 1;
				this.pickList = [];
				this.emptyShow = false
				this.getPickList();
				uni.removeStorageSync('paySuccess')
			}
		},
		onReachBottom() {
			if (this.loadStatus == "more")
				(this.loadStatus = "loading"), this.getCardList();
		},
		methods: {
			// 返回
			backClick() {
				uni.navigateBack({

				})
			},
			//搜索
			search() {
				this.cardList = []
				this.page = 1
				this.loadStatus = "loading"
				this.emptyShow = false
				this.getCardList()
			},
			cancelOrder(id) {
				serviceCancelOrder({
					order_id: id
				}).then(res => {
					uni.showToast({
						icon: "none",
						title: res.msg
					})
					this.cardList = []
					this.page = 1
					this.loadStatus = "loading"
					this.emptyShow = false
					this.getCardList()
				})
			},
			changeTab(index) {
				this.current = index;
				this.cardList = []
				this.page = 1
				this.loadStatus = "loading"
				this.emptyShow = false
				this.getCardList()
			},
			getCardList() {
				serviceUseRecord({
					type: Number(this.current) + 1,
					search_str: this.keys,
					page: this.page,
					limit: this.limit
				}).then(res => {
					uni.hideLoading()
					this.loadStatus = "more";
					this.cardList.push.apply(this.cardList, res.data);
					if (res.data.length < this.limit) this.loadStatus = "noMore"; //判断所有数据是否加载完成；
					this.page = this.page + 1;
					if (this.cardList.length == 0) {
						this.contentText.contentnomore = ''
						this.emptyShow = true
					} else {
						this.contentText.contentnomore = '没有更多了'
					}
				})
			},
			//跳转服务卡记录详情
			goDetail(item) {
				uni.navigateTo({
					url: "./serviceRecordDetail?item=" + encodeURIComponent(JSON.stringify(item))
				})
			},
			goPay(id) {
				this.order_id = id
				//#ifndef APP-PLUS
				this.submit(id)
				//#endif
				//#ifdef APP-PLUS
				this.$refs.choosePay.show()
				//#endif
			},
			confirm(e) {
				if(e.payType == "weixin") {
					this.paytype = 1
				} else {
					this.paytype = 2
				}
				this.provider = e.provider
				this.submit(this.order_id)
			},
			// 立即支付
			submit(id) {
				let _this = this
				uni.showLoading({
					mask:true
				})
				servicePay({
					order_id: id,
					//#ifdef MP-WEIXIN
					source: 1,
					//#endif
					//#ifdef APP-PLUS
					source: 2,
					//#endif
					//#ifdef H5
					source: 3,
					//#endif
					pay_type: this.paytype,
				}).then(res => {
					// #ifdef MP-WEIXIN
					uni.requestPayment({
						provider: 'wxpay',
						timeStamp: res.data.timestamp,
						nonceStr: res.data.nonceStr,
						package: res.data.package,
						signType: res.data.signType,
						paySign: res.data.paySign,
						success: function(re) {
							_this.cardList = []
							_this.page = 1
							_this.loadStatus = "loading"
							_this.emptyShow = false
							_this.getCardList()
						}
					});
					// #endif
					// #ifdef APP-PLUS
					uni.requestPayment({
						provider: this.provider,
						orderInfo: res.data,
						success: function(re) {
							_this.cardList = []
							_this.page = 1
							_this.loadStatus = "loading"
							_this.emptyShow = false
							_this.getCardList()
						},
						fail: (e) => {
							console.log(e)
						}
					});
					// #endif
					//#ifdef H5
					// h5支付
					let url = "/pages/center/vip/serviceRecord"
					wechat.pay(res.data, url, false)
					//#endif
				})
			},

			//联系客服
			phoneCall() {
				uni.makePhoneCall({
					phoneNumber: uni.getStorageSync("siteSerPhone")
				})
			},

		}
	}
</script>
<style>
	page {
		background-color: #fafafa;
	}
</style>
<style lang="scss" scoped>
	.container {
		.searchBox {
			position: relative;

			.searchIcon {
				position: absolute;
				left: 20rpx;
				//#ifdef MP-WEIXIN
				top: 14rpx;
				//#endif
				//#ifdef APP-PLUS
				top: -14rpx;
				//#endif
				//#ifdef H5
				top: -8rpx;
				//#endif
				color: #9a9a9a;
			}

			.topSearch {
				background-color: #f8f8f8;
				//#ifdef MP-WEIXIN
				width: 420rpx;
				//#endif
				//#ifdef APP-PLUS
				width: 620rpx;
				//#endif
				height: 68rpx;
				padding-left: 80rpx;
				box-sizing: border-box;
				line-height: 68rpx;
				font-size: 24rpx;
				border-radius: 34rpx;
			}
		}

		.content {
			.tabBox {
				position: sticky;
				// #ifdef APP-PLUS
				top: calc(44px + var(--status-bar-height));
				// #endif
				// #ifdef H5
				top: 44px;
				// #endif
				display: flex;
				height: 88rpx;
				z-index: 9999999999;
				background-color: #fff;
				box-shadow: 0px 6rpx 8rpx 0px rgba(0, 0, 0, 0.05);

				.tab {
					width: 50%;
					text-align: center;
					line-height: 88rpx;
					color: #484848;
					font-size: 30rpx;
				}

				.active {
					font-weight: bold;
					color: #484848;
					position: relative;
				}

				.active:after {
					content: '';
					position: absolute;
					height: 4rpx;
					width: 50rpx;
					border-radius: 2rpx;
					background-color: #FBC866;
					bottom: 10rpx;
					left: 50%;
					margin-left: -25rpx;
				}
			}

			.order-item {
				display: flex;
				flex-direction: column;
				margin: 20rpx 30rpx;
				padding: 0 30rpx;
				background: #fff;
				border-radius: 10rpx;
				box-shadow: 0 0 20rpx rgba(0, 0, 0, .05);

				.i-top {
					display: flex;
					justify-content: space-between;
					align-items: baseline;
					height: 80rpx;
					line-height: 80rpx;
					font-size: 32rpx;
					font-weight: bold;
					color: #343434;

					.seller_name {
						max-width: 400rpx;
					}

					.state {
						font-size: 24rpx;
						font-weight: 500;
						font-weight: normal;
						color: #FBC866;
					}
				}

				.cardType {
					display: flex;
					align-items: baseline;
					color: #333;
					padding: 6rpx 0 20rpx;
					font-size: 28rpx;
					color: #666666;

					.a {
						margin-right: 8rpx;
					}
				}

				.timeGroup {
					padding-bottom: 30rpx;

					.a {
						display: flex;
						align-items: baseline;
						font-size: 26rpx;
						line-height: 50rpx;

						.a-a {
							color: #333;
						}

						.a-b {
							color: #999;
						}
					}
				}

				.price {
					display: flex;
					align-items: baseline;
					justify-content: flex-end;
					color: #666;

					.a {
						font-size: 28rpx;
						margin-right: 6rpx;
					}

					.b {
						font-weight: bold;
						font-size: 36rpx;

						&:before {
							font-weight: bold;
							content: '￥';
							font-size: 24rpx;
						}
					}
				}

				.action-box {
					display: flex;
					align-items: center;
					height: 120rpx;
					position: relative;

					.action-btn {
						width: 130rpx;
						height: 50rpx;
						margin: 0;
						margin-right: 20rpx;
						padding: 0;
						text-align: center;
						line-height: 50rpx;
						font-size: 24rpx;
						color: #666;
						background: #fff;
						border-radius: 100px;
						border: 2rpx solid #eee;
					}

					.act {
						border: none;
						background: #FBCC2C;
						border: 2rpx solid #FBCC2C;
					}
				}
			}

			.serviceRecod {
				width: 690rpx;
				height: 260rpx;
				box-sizing: border-box;
				margin: 20rpx 30rpx;
				padding: 30rpx;
				border-radius: 10rpx;
				box-shadow: 0 0 20rpx rgba(0, 0, 0, .05);
				display: flex;
				flex-direction: column;
				justify-content: space-between;
				position: relative;

				.cardbg {
					position: absolute;
					top: 0;
					left: 0;
					width: 100%;
					height: 100%;
					//#ifndef H5
					z-index: -1;
					//#endif
				}

				.i-top {
					display: flex;
					justify-content: space-between;
					align-items: baseline;
					font-weight: bold;
					color: #FFFFFF;
					padding-bottom: 16rpx;
					//#ifdef H5
					position: relative;
					//#endif

					.seller_name {
						font-size: 42rpx;
					}

					.b {
						font-size: 24rpx;
					}
				}

				.timeGroup {
					font-size: 30rpx;
					color: #FFFFFF;
					//#ifdef H5
					position: relative;
					//#endif

					.a {
						line-height: 54rpx;
						display: flex;
						justify-content: space-between;
					}
				}
			}
		}
	}
</style>
